<?php

Globals::requireClass('Controller');
Globals::requireTable('Delivery');
Globals::requireTable('DeliveryPoint');
Globals::requireTable('ServiceMode');
Globals::requireTable('Cost');
Globals::requireTable('Order');
Globals::requireTable('User');

class IndexController extends Controller {

    protected $DeliveryTable;
    protected $DeliveryPointTable;
    protected $ServiceModeTable;
    protected $OrderTable;
    protected $CostTable;
    protected $UserTable;
    public static $defaultConfig = array(
        'viewEnabled' => true,
        'layoutEnabled' => true,
        'title' => null
    );

    public function __construct($config = null) {
        parent::__construct($config);
        $this->DeliveryTable = new DeliveryTable($config);
        $this->DeliveryPointTable = new DeliveryPointTable($config);
        $this->ServiceModeTable = new ServiceModeTable($config);
        $this->CostTable = new CostTable($config);
        $this->OrderTable = new OrderTable($config);
        $this->UserTable = new UserTable($config);
    }

    public function indexAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        // var_dump($_SESSION);exit;
        $this->config['layoutEnabled'] = false;
    }

    public function mainAction() {
        $this->config['layoutEnabled'] = false;
    }

    public function test2Action() {
        //$this->config['layoutEnabled'] = false;
    }

    public function SendSingleAction() {
        if (!in_array(8, $_SESSION['userinfo']['acl'])) {
            $this->redirect("./?m=index&a=main");
            exit;
        }
        $this->config['layoutEnabled'] = false;
        $status = $this->getIntParam('status');
        if ($status) {
            $where[] = "`status` = $status";
        } else {
            $where[] = "`status` > 0";
        }
        $count = $this->DeliveryTable->listCount($where);
        $pageId = $this->getIntParam('page');
        $pageSize = 5;
        $this->view->paging = $this->getPaging($count, $pageSize, $pageId);
        $data = $this->DeliveryTable->listPage($where, "id desc", $pageSize, $pageId);
        $this->view->status = $status;
        //var_dump($data);exit;
        $this->view->data = isset($data) ? $data : array();
    }

    public function dosendsingleAction() {
        $this->config['layoutEnabled'] = false;
        $name = $this->getParam('name');
        $describe = $this->getParam('describe');
        $time = date("Y-m-d H:i:s");
        $user_id = $_SESSION['userinfo']['id'];
        $unqiue = $this->DeliveryTable->getRow("`name` = '{$name}' AND `status` > 0");
        if ($unqiue) {
            echo -1;
            exit;
        }
        $theid = $this->DeliveryTable->add(array('`name`' => $name, '`describe`' => $describe, '`createtime`' => $time, '`user_id`' => $user_id), true);
        echo $theid;
        exit;
    }

    public function sendsingleupdateAction() {
        $id = $this->getIntParam('id');
        $data = $this->DeliveryTable->getRow("`id` = $id");
        echo json_encode($data);
        exit;
    }

    public function sendsingledormancyAction() {
        $id = $this->getIntParam('id');
        $this->DeliveryTable->update(array("`status`" => 2), $id);
        echo $id;
        exit;
    }

    public function sendsingleactivationAction() {
        $id = $this->getParam('id');
        $this->DeliveryTable->update(array("`status`" => 1), $id);
        echo $id;
        exit;
    }

    public function dosendsingleupdateAction() {
        $id = $this->getIntParam("id");
        $name = $this->getParam('name');
        $describe = $this->getParam('describe');
        $unqiue = $this->DeliveryTable->getRow("`name` = '{$name}' AND `status` > 0");
        $slim = $this->DeliveryTable->getRow($id);
        if ($unqiue && $slim['name'] != $name) {
            echo -1;
            exit;
        }
        $this->DeliveryTable->update(array('`name`' => $name, '`describe`' => $describe), $id);
        echo 1;
        exit;
    }

    /*     * ************ */

    public function deliverypointAction() {
        if (!in_array(7, $_SESSION['userinfo']['acl'])) {
            $this->redirect("./?m=index&a=main");
            exit;
        }
        $this->config['layoutEnabled'] = false;
        $status = $this->getIntParam('status');
        $point = $this->getParam('point');
        if ($status) {
            $where[] = "`status` = $status";
        } else {
            $where[] = "`status` > 0";
        }
        if (isset($point)) {
            $where[] = "`point` like '%{$point}%'";
        }
        $count = $this->DeliveryPointTable->listCount($where);
        $pageId = $this->getIntParam('page');
        $pageSize = 15;
        $this->view->paging = $this->getPaging($count, $pageSize, $pageId);
        $data = $this->DeliveryPointTable->listPage($where, "id desc", $pageSize, $pageId);
        $this->view->status = $status;
        $this->view->point = $point;
        $this->view->data = isset($data) ? $data : array();
    }

    public function dodeliverypointAction() {
        if (!in_array(7, $_SESSION['userinfo']['acl'])) {
            $this->redirect("./?m=index&a=main");
            exit;
        }
        $point = $this->getParam('point');
        $describe = $this->getParam('describe');
        $unqiue = $this->DeliveryPointTable->getRow("`point` = '{$point}' AND `status` > 0");
        if ($unqiue) {
            echo -1;
            exit;
        }
        $time = date("Y-m-d H:i:s");
        $user_id = $_SESSION['userinfo']['id'];
        $theid = $this->DeliveryPointTable->add(array("`point`" => $point, "`describe`" => $describe, "`user_id`" => $user_id, "`createtime`" => $time));
        echo $theid;
        exit;
    }

    public function deliverypointupdateAction() {
        $id = $this->getIntParam('id');
        $data = $this->DeliveryPointTable->getRow($id);
        echo json_encode($data);
        exit;
    }

    public function dodeliverypointupdateAction() {
        $id = $this->getParam("id");
        $point = $this->getParam('point');
        $describe = $this->getParam('describe');
        $unique = $this->DeliveryPointTable->getRow("`point` = '{$point}' AND `status` > 0");
        $slim = $this->DeliveryPointTable->getRow($id);
        if ($unique && $slim['point'] != $point) {
            echo -1;
            exit;
        }
        $this->DeliveryPointTable->update(array("`point`" => $point, "`describe`" => $describe), $id);
        echo $id;
        exit;
    }

    public function deliverypointdeleteAction() {
        $id = $this->getParam("id");
        if ($id < 1) {
            echo -1;
            exit;
        }
        $this->DeliveryPointTable->update(array("`status` = 0"), $id);
        echo 1;
        exit;
    }

    public function sendsingledeleteAction() {
        $id = $this->getIntParam('id');
        if ($id < 1) {
            echo -1;
        }
        $this->DeliveryTable->update(array('`status`' => 0), $id);
        echo 1;
        exit;
    }

    /*     * ****业务方式******** */

    public function servicemodeAction() {
        if (!in_array(6, $_SESSION['userinfo']['acl'])) {
            $this->redirect("./?m=index&a=main");
            exit;
        }
        $this->config['layoutEnabled'] = false;
        $status = $this->getIntParam('status');
        $servicemode = $this->getParam('servicemode');
        if ($status > 0) {
            $where[] = "`status` = $status";
        } else {
            $where[] = "`status` > 0";
        }
        if (isset($servicemode)) {
            $where[] = "`servicemode` like '%{$servicemode}%'";
        }
        $count = $this->ServiceModeTable->listCount($where);
        $pageId = $this->getIntParam('page');
        $pageSize = 15;
        $this->view->paging = $this->getPaging($count, $pageSize, $pageId);
        $data = $this->ServiceModeTable->listPage($where, "id desc", $pageSize, $pageId);
        $this->view->status = $status;
        $this->view->servicemode = $servicemode;
        $this->view->data = isset($data) ? $data : array();
    }

    public function servicemodedormancyAction() {
        $id = $this->getIntParam('id');
        $this->ServiceModeTable->update(array("`status`" => 2), $id);
        echo $id;
        exit;
    }

    public function servicemodeactivationAction() {
        $id = $this->getIntParam('id');
        $this->ServiceModeTable->update(array("`status`" => 1), $id);
        echo $id;
        exit;
    }

    public function doservicemodeAction() {
        $servicemode = $this->getParam('servicemode');
        $pingying = $this->getParam('pingying');
        $user_id = $_SESSION['userinfo']['id'];
        $createtime = date("Y-m-d H:i:s");
        $unqiue = $this->ServiceModeTable->getRow("`servicemode` = '{$servicemode}' AND `status` > 0");
        if ($unqiue) {
            echo -1;
            exit;
        }
        $ID = $this->ServiceModeTable->add(array("`servicemode`" => $servicemode, "`pingying`" => $pingying, "`user_id`" => $user_id, "`createtime`" => $createtime), true);
        echo $ID;
        exit;
    }

    public function servicemodejsonAction() {
        $id = $this->getIntParam('id');
        $data = $this->ServiceModeTable->getRow($id);
        echo json_encode($data);
        exit;
    }

    public function doservicemodeupdateAction() {
        $id = $this->getIntParam('id');
        $servicemode = $this->getParam('servicemode');
        $pingying = $this->getParam('pingying');
        $unqiue = $this->ServiceModeTable->getRow("`servicemode` = '{$servicemode}' AND `status` > 0");
        $slim = $this->ServiceModeTable->getRow($id);
        if ($unqiue && $slim['servicemode'] != $servicemode) {
            echo -1;
            exit;
        }
        $this->ServiceModeTable->update(array("`servicemode`" => $servicemode, "`pingying`" => $pingying), $id);
        echo $id;
        exit;
    }

    public function servicemodedeleteAction() {
        $id = $this->getIntParam('id');
        $this->ServiceModeTable->update(array("`status`" => 0), $id);
        echo $id;
        exit;
    }

    public function costinformationAction() {
        if (!in_array(5, $_SESSION['userinfo']['acl'])) {
            $this->redirect("./?m=index&a=main");
            exit;
        }
        $name = $this->getParam('name');
        $number = $this->getParam('number');
        $type = $this->getParam('type');
        $status = $this->getIntParam('status');
        switch ($type) {
            case 1:
                $where = array("`type`" => 1); //应收
                break;
            case 2:
                $where = array("`type`" => 0); //应付
                break;
            default:
                $where = array();
                break;
        }
        if ($status > 0) {
            $where[] = "`status` = $status";
        } else {
            $where[] = "`status` > 0";
        }
        if (isset($number)) {
            $where[] = "`number` like '%{$number}%'";
        }
        if (isset($name)) {
            $where[] = "`name` like '%{$name}%'";
        }
        $count = $this->CostTable->listCount($where);
        $pageId = $this->getIntParam('page');
        $pageSize = 15;
        $this->view->paging = $this->getPaging($count, $pageSize, $pageId);
        $data = $this->CostTable->listPage($where, "id desc", $pageSize, $pageId);
        $this->view->name = $name;
        $this->view->number = $number;
        $this->view->type = $type;
        $this->view->status = $status;
        $this->view->data = isset($data) ? $data : array();
    }

    public function costdormancyAction() {
        $id = $this->getParam('id');
        $this->CostTable->update(array("`status`" => 2), $id);
        echo $id;
        exit;
    }

    public function costactivationAction() {
        $id = $this->getParam('id');
        $this->CostTable->update(array("`status`" => 1), $id);
        echo $id;
        exit;
    }

    public function docostAction() {
        $name = $this->getParam('name');
        $number = $this->getParam('number');
        $defaultcost = $this->getParam('defaultcost');
        $is_automaticint = $this->getParam('is_automaticint');
        $is_paytaxes = $this->getParam('is_paytaxes');
        $is_profit = $this->getParam('is_profit');
        $type = $this->getParam('type');
        $unique = $this->CostTable->getRow("`name` = '{$name}' AND `status` > 0");
        if ($unique) {
            echo -1;
            exit;
        }
        $arr = array(
            "`name`" => $name,
            "`number`" => $number,
            "`defaultcost`" => $defaultcost,
            "`is_automaticint`" => $is_automaticint,
            "`is_paytaxes`" => $is_paytaxes,
            "is_profit" => $is_profit,
            "`type`" => $type,
            "`createtime`" => date("Y-m-d H:i:s"),
            "`user_id`" => $_SESSION['userinfo']['id']
        );
        $ID = $this->CostTable->add($arr, true);
        echo $ID;
        exit;
    }

    public function costjsonAction() {
        $id = $this->getIntParam('id');
        $data = $this->CostTable->getRow($id);
        echo json_encode($data);
        exit;
    }

    public function costupdateAction() {
        $id = $this->getIntParam('id');
        $name = $this->getParam('name');
        $number = $this->getParam('number');
        $defaultcost = $this->getParam('defaultcost');
        $is_automaticint = $this->getParam('is_automaticint');
        $is_paytaxes = $this->getParam('is_paytaxes');
        $is_profit = $this->getParam('is_profit');
        $type = $this->getParam('type');
        $unique = $this->CostTable->getRow("`name` = '{$name}' AND `status` > 0");
        $slim = $this->CostTable->getRow($id);
        if ($unique && $slim['name'] != $name) {
            echo -1;
            exit;
        }
        $arr = array(
            "`name`" => $name,
            "`number`" => $number,
            "`defaultcost`" => $defaultcost,
            "`is_automaticint`" => $is_automaticint,
            "`is_paytaxes`" => $is_paytaxes,
            "is_profit" => $is_profit,
            "`type`" => $type
        );
        $this->CostTable->update($arr, $id);
        echo $id;
        exit;
    }

    public function costdeleteAction() {
        $id = $this->getParam('id');
        $this->CostTable->update(array("`status`" => 0), $id);
        echo $id;
        exit;
    }

    /*     * ********** */

    public function deliverypointdormancyAction() {
        $id = $this->getIntParam('id');
        $this->DeliveryPointTable->update(array("`status`" => 2), $id);
        echo $id;
        exit;
    }

    public function deliverypointactivationAction() {
        $id = $this->getIntParam('id');
        $this->DeliveryPointTable->update(array("`status`" => 1), $id);
        echo $id;
        exit;
    }

    public function gungunAction() {
        $date = date("Ymd");
        $order = substr($date, 2);
        $data = $this->OrderTable->listAll("`orderid` like '%1705%'");
        foreach ($data as $k => $v) {
            $user_id[] = $v['user_id'];
        }
        $user_id = array_unique($user_id);
        $user_ar = implode(",", $user_id);
        $user = $this->UserTable->listAll("`id` in ({$user_ar})");
        foreach ($user as $k => $v) {
            $user_data[$v['id']] = $v['username'];
        }
        foreach($data as $k=>$v){
            $result[] = $user_data[$v['user_id']]."使用了".$v['count']."个箱子。。";
        }
        echo json_encode($result);
        exit;
    }

    protected function out() {
        $this->layout->nav = 'index';
        parent::out();
    }

}

Config::extend('IndexController', 'Controller');
